package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.example.config.RocketMQConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/batch")
public class BatchSendController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private RocketMQConfigProperties prop;

    @RequestMapping("/send")
    public String send() {
        List<Message<String>> messages = new ArrayList<>();
        // 构建消息1
        Message<String> message1 = MessageBuilder.withPayload("Message 1").build();
        messages.add(message1);
        // 构建消息2
        Message<String> message2 = MessageBuilder.withPayload("Message 2").build();
        messages.add(message2);
        // 构建消息3
        Message<String> message3 = MessageBuilder.withPayload("Message 3").build();
        messages.add(message3);
        // 批量发送消息
        rocketMQTemplate.syncSend(prop.getTopic().get("batch"), messages);
        return "ok";
    }
}
